package com.ambulance.project.biz.controller.treatweb;


import com.ambulance.framework.security.service.SysLoginService;
import com.ambulance.framework.web.domain.AjaxResult;
import com.ambulance.framework.web.domain.AjaxResultData;
import com.ambulance.project.biz.dto.treat.TreatUserInfo;
import com.ambulance.project.biz.enums.Source;
import com.ambulance.project.biz.service.SysCacheService;
import com.ambulance.project.biz.service.WebCustomerService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 人员信息 前端控制器
 * </p>
 *
 * @author zr
 * @since 2020-04-21
 */
@Api(tags = "会诊子系统Web-登陆")
@RestController
@RequestMapping(value = "/treat/web/v1/user", method = RequestMethod.POST)
public class TreatWebUserController {


    @Autowired
    private SysLoginService sysLoginService;

    @Autowired
    private WebCustomerService webCustomerService;
    @Autowired
    private SysCacheService sysCacheService;

    /**
     * web会诊-发送验证码
     *
     * @param phone
     * @return
     */
    @ApiOperation(value = "发送验证码", notes = "发送验证码", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "string"),

    })
    @ApiResponses({
            @ApiResponse(code = 200, response = AjaxResultData.class, message = "响应")
    })
    @RequestMapping(value = "/send/code")
    public Object preLogin(@RequestParam(value = "phone") String phone
    ) {
        sysCacheService.sendCode(phone);
        return AjaxResult.success();
    }

    /**
     * web会诊-登陆
     *
     * @param
     * @return
     */
    @ApiOperation(value = "登陆", notes = "登陆", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账号", dataType = "string"),
            @ApiImplicitParam(name = "password", value = "密码", dataType = "string"),
            @ApiImplicitParam(name = "type", value = "登陆方式,1手机验证码，2账户密码", dataType = "string"),

    })
    @ApiResponses({
            @ApiResponse(code = 200, response = AjaxResultData.class, message = "响应")
    })
    @RequestMapping(value = "/login")
    public Object login(@RequestParam(value = "account") String account,
                        @RequestParam(value = "password") String password,
                        @RequestParam(value = "type", defaultValue = "2", required = false) String type
    ) {
        String token = null;
        if (type.equals("2")) {
            token = sysLoginService.loginNoCaptcha(account, password, Source.WYD);
            return AjaxResult.success(token);
        }
        if ("1".equals(type)) {
            token = sysLoginService.loginNoCaptcha(account, password, Source.TER);
            return AjaxResult.success(token);
        }

        return AjaxResult.error();

    }

    @ApiOperation(value = "登陆用户信息", notes = "登陆用户信息", produces = "application/json")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 200, response = TreatUserInfo.class, message = "响应")
    })
    @RequestMapping(value = "/info")
    public Object loginUserInfo(
    ) {

        return AjaxResult.success(webCustomerService.getTreatUserInfo());
    }


}
